package com.tyq.servlet;

import com.tyq.util.DocumentUtils;
import com.tyq.util.HttpConnection;
import com.tyq.util.Utils;
import org.w3c.dom.Document;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.net.HttpURLConnection;

/**
 * 请求sayHello
 * Created by tyq on 16/10/16.
 */
public class SayHelloServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
        doPost(req,resp);
    }

    /**
     * 客户端实现
     * @param req
     * @param resp
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
        String name = req.getParameter("name");
        String content = req.getParameter("content");
        String data = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://service.tyq.com/\">" +
                "   <soapenv:Header/>" +
                "   <soapenv:Body>" +
                "      <ser:sayHello>" +
                "         <arg0>"+name+"</arg0>" +
                "         <arg1>"+content+"</arg1>" +
                "      </ser:sayHello>" +
                "   </soapenv:Body>" +
                "</soapenv:Envelope>";
        String url = "http://127.0.0.1:8080/ws/student";
        try {
            InputStream is = HttpConnection.sendConnection(data,url);
            Document document = DocumentUtils.getDocumentByInput(is);
            //获取name节点值
            String name1 = DocumentUtils.getNodeValue(document, "name");
            String message = "{'name':'"+name1+"'}";
            //返回消息至页面
            Utils.responseAjax(resp,message);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
